//
//  DeviceControlBaseViewController.m
//  YSSmartHome
//
//  Created by yanghao on 03/05/2017.
//  Copyright © 2017 yahaong. All rights reserved.
//

#import "DeviceControlBaseViewController.h"
#import "YSCommon.h"
#import "UIImage+Extension.h"

@interface DeviceControlBaseViewController ()
@property (nullable, nonatomic, strong) UIView* splitView;
@end

@implementation DeviceControlBaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupNavBar];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}

- (void)hiddenSplitView:(BOOL)hidden {
    self.splitView.hidden = hidden;
}

#pragma mark - event response

- (void)backPreviousVC {
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - private method

- (void)setupNavBar {
    [self.view addSubview:self.navigationBar];
    [self.navigationBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.left.equalTo(self.view);
        make.height.mas_equalTo(64);
    }];
}

#pragma mark - getters and setters 

- (UINavigationBar *)navigationBar {
    if (!_navigationBar) {
        _navigationBar = [[UINavigationBar alloc]init];
        [_navigationBar setBackgroundImage:[UIImage yh_imageWithColor:RGBColor(0, 111, 195)] forBarMetrics:UIBarMetricsDefault];
        NSMutableDictionary *barTextAttrs = [NSMutableDictionary dictionary];
        barTextAttrs[NSForegroundColorAttributeName] = [UIColor whiteColor];
        barTextAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:17];
        [_navigationBar setTitleTextAttributes:barTextAttrs];
        _navigationBar.items = @[self.titleItem];
        [_navigationBar addSubview:self.splitView];
        [self.splitView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.leading.trailing.bottom.equalTo(_navigationBar);
            make.height.equalTo(1);
        }];
    }
    return _navigationBar;
}

- (UIView *)splitView {
    if (!_splitView) {
        _splitView = [[UIView alloc]init];
        _splitView.backgroundColor = RGBColor(13, 97, 161);
    }
    return _splitView;
}

- (UINavigationItem *)titleItem {
    if (!_titleItem) {
        _titleItem = [[UINavigationItem alloc]init];
        UIButton *button = [[UIButton alloc]init];
        [button addTarget:self action:@selector(backPreviousVC) forControlEvents:UIControlEventTouchUpInside];
        UIImage *image = [UIImage imageNamed:@"back_white"];
        [button setImage:image forState:UIControlStateNormal];
        [button setImage:image forState:UIControlStateHighlighted];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [button sizeToFit];
        button.contentEdgeInsets = UIEdgeInsetsMake(0, -20, 0, 0);
        _titleItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:button];
    }
    return _titleItem;
}

@end
